﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace WriteBugs.Controls
{
    public class SpacingAdorner : Adorner
    {
        private readonly ContentPresenter _contentPresenter;
        private Control Control
        {
            get
            {
                return (Control)this.AdornedElement;
            }
        }

        public SpacingAdorner(UIElement adornedElement, int Index) : base(adornedElement)
        {
            IsHitTestVisible = false;

            this._contentPresenter = new ContentPresenter
            {
                Content = new Border
                {
                    Background = Brushes.DarkGray,  
                    Width = this.Control.ActualWidth - 10,
                    Height = 4,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Top,
                    CornerRadius = new CornerRadius(2),
                },
            };

            this.Margin = new Thickness(0, Index * 35 + 2, 0, 0);
        }

        #region Override

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }

        protected override Visual GetVisualChild(int index) // replace the Visual of the TextBox with the visual of the _contentPresenter;
        {
            return this._contentPresenter;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            this._contentPresenter.Measure(this.Control.RenderSize); // delegate the measure override to the ContentPresenter's Measure
            return this.Control.RenderSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            this._contentPresenter.Arrange(new Rect(finalSize));
            return finalSize;
        }

        #endregion Override
    }
}
